﻿namespace PowerSong.UserInterface
    {
    using System;
    using System.IO;
    using System.Windows;
    using PowerSong.Import;
    using PowerSong.SongDatabase;
    using PowerSong.UserInterface.Tasks;

    /// <summary>
    /// Interaction logic for ConvertOldDatabaseWindow.xaml
    /// </summary>
    public partial class ConvertOldDatabaseWindow : Window
        {

        public string TargetFileName { get; private set; }

        public ConvertOldDatabaseWindow()
            {
            InitializeComponent();
            this.TargetFileName = null;
            }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
            {
            this.DialogResult = false;
            }

        private void OKButton_Click(object sender, RoutedEventArgs e)
            {

            try
                {

                // Ensure the source folder has been specified
                if (string.IsNullOrEmpty(LocationTextBox.FolderName.Trim()))
                    {
                    throw new Exception("Please specify the location of the old PowerSong 1.0 database that you wish to convert.");
                    }

                // Ensure the source folder exists
                if (!Directory.Exists(LocationTextBox.FolderName))
                    {
                    throw new Exception("The location '" + LocationTextBox.FolderName + "' does not exist. Please specify the correct location of an old PowerSong database folder.");
                    }

                // Ensure the target file has been specified
                if (string.IsNullOrEmpty(TargetFileNameTextBox.FileName))
                    {
                    throw new Exception("Please specify the file name of the database that you wish to create.");
                    }

                // Ensure the target file does not already exist
                if (File.Exists(TargetFileNameTextBox.FileName))
                    {
                    var proceed = Utilities.Confirm("Target File Exists", "The target file '" + TargetFileNameTextBox.FileName + "' already exists. Do you wish to overwrite the file?");
                    if (!proceed) return;
                    }

                // The database must be a valid legacy PowerSong database
                if (!PowerSong.SongDatabase.Legacy.Database.IsValidDatabase(LocationTextBox.FolderName))
                    {
                    throw new Exception("The specified location '" + LocationTextBox.FolderName + "' does not appear to contain a valid PowerSong database.");
                    }

                // Create the target database
                var target = Database.Create(TargetFileNameTextBox.FileName);

                // Perform the conversion
                var importer = new LegacyDatabaseImport(target, LocationTextBox.FolderName);
                var task = new ImportTask(importer, null, "Converting the specified database into a modern PowerSong database...");
                ProgressWindow.Start(task);
                
                // Show messages if any were generated
                if (task.Messages.Length > 0)
                    {
                    MessagesWindow.Show("The following issues were encountered during the conversion. Please review these messages before continuing.", task.Messages);
                    }

                // All done!
                Utilities.ShowInformationMessage("Database Upgraded",
                                                 "The database has been upgraded.",
                                                 "The database is now kept at '" + TargetFileNameTextBox.FileName + "'");

                // Set database file name variable, but only if the user specified that they want to open the database immediately
                if (UseDatabaseCheckBox.IsChecked == true)
                    {
                    this.TargetFileName = TargetFileNameTextBox.FileName;
                    }

                // Close the window
                this.DialogResult = true;

                }
            catch (Exception ex)
                {
                Utilities.ShowErrorMessage("Could Not Convert Database", ex.Message);
                }

            }

        private void LocationTextBox_FolderNameChanged(object sender, EventArgs e)
            {
            try
                {
                var folderName = Path.GetDirectoryName(LocationTextBox.FolderName);
                var fileName = Path.GetFileName(LocationTextBox.FolderName) + ".PowerSong";
                TargetFileNameTextBox.FileName = Path.Combine(folderName, fileName);
                }
            catch { }
            }

        }

    }
